package com.neuedu.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.entity.RegistLevel;
import com.neuedu.mapper.RegistLevelMapper;
import com.neuedu.service.RegistLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 挂号级别表 服务实现类
 * </p>
 *
 * @author LiYang
 * @since 2024-11-07
 */
@Service
public class RegistLevelServiceImpl extends ServiceImpl<RegistLevelMapper, RegistLevel> implements RegistLevelService {

    @Override
    public IPage<RegistLevel> list(Integer pageNo, Integer pageSize, String name) {
        QueryWrapper<RegistLevel> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(name)) {
            wrapper.like("name", name);
        }
        return this.page(new Page<>(pageNo, pageSize), wrapper);
    }

    @Override
    @CacheEvict(value = "admin", key = "'regist'")
    public boolean add(String code, String name, Integer fee, Integer quota, Integer sequenceNo) {
        return this.save(new RegistLevel(code, name, fee, quota, sequenceNo));
    }

    @Override
    @CacheEvict(value = "admin", key = "'regist'")
    public boolean update(Long id, String code, String name, Integer fee, Integer quota, Integer sequenceNo) {
        return this.updateById(new RegistLevel(id, code, name, fee, quota, sequenceNo));
    }

    @Override
    @CacheEvict(value = "admin", key = "'regist'")
    public boolean del(Long id, Boolean delmark) {
        return this.updateById(new RegistLevel(id, delmark));
    }

    @Override
    public boolean check(String field, String value, Long id) {
        QueryWrapper<RegistLevel> wrapper = new QueryWrapper<>();
        wrapper.eq(field, value);
        if (null != id) {
            wrapper.ne("id", id);
        }
        return this.count(wrapper) == 0;
    }

    @Override
    @Cacheable(value = "admin", key = "'regist'")
    public List<RegistLevel> getActive() {
        QueryWrapper<RegistLevel> wrapper = new QueryWrapper<>();
        wrapper.eq("delmark", 1);
        return this.list(wrapper);
    }
}
